#!/bin/sh
alias GETTEXT='gettext "kylin-system-verify"'

PREREQS=""

prereqs() { echo "$PREREQS"; }

case "$1" in
    prereqs)
    prereqs
    exit 0
    ;;
esac


user=$(whoami)

if [ $user != "root" ];then
	: '
	echo "请使用sudo或切换到root用户执行该程序"
	'
	echo "$(GETTEXT "Use sudo or switch to root to execute the program")"
	exit 1;
fi

ROOTFS_MOUNT=$(awk '!/^rootfs / {if ($2 == "/") {print $1 " " $2 " " $3 " " $4; exit}}' /proc/mounts)
ROOTFS_OPTIONS=$(echo $ROOTFS_MOUNT | awk '{ print $4 }')
ROOTFS_FSTYPE=$(echo $ROOTFS_MOUNT | awk '{ print $3 }')
ROOTFS_DEVICE=$(echo $ROOTFS_MOUNT | awk '{ print $1 }')
echo $ROOTFS_OPTIONS | grep -qe '^ro,' && readonly="y" || readonly="n"

export ROOTFS_DEVICE

SHOW_SERIAL_NO="y"
export SHOW_SERIAL_NO

CODE_NO_HYPHEN="y"
export CODE_NO_HYPHEN

stay_message() {
	msg="${1}"

	echo ""
	echo $msg
	while true
	do
		sleep 100
	done
}

ky_activate_retry() {
	echo ""
	if [ $1 = 1 ] || [ $1 = 2 ] || [ $1 = 3 ]; then
		: '
		stay_message "无有效LICENSE文件"
		'
		stay_message "$(GETTEXT "No valid LICENSE file exists")"
	elif [ $1 = 4 ]; then
		: '
		stay_message "无有效麒麟信息文件"
		'
		stay_message "$(GETTEXT "No valid .kyinfo file exists")"
	elif [ $1 = 14 ]; then
		: '
		stay_message "无有效公钥"
		'
		stay_message "$(GETTEXT "No valid public key")"
	elif [ $1 = 15 ]; then
		: '
		stay_message "系统时间有问题"
		'
		stay_message "$(GETTEXT "There is a system time problem")"
	elif [ $1 = 16 ]; then
		: '
		stay_message "GPG验证出现问题"
		'
		stay_message "$(GETTEXT "GPG authentication is faulty")"
	elif [ $1 = 17 ]; then
		: '
		stay_message "无法获取有效信息"
		'
		stay_message "$(GETTEXT "Unable to obtain valid information")"
	elif [ $1 = 8 ] || [ $1 = 9 ] || [ $1 = 10 ] || [ $1 = 11 ] || [ $1 = 12 ] || [ $1 = 13 ]; then
		: '
		echo "请重试："
		'
		echo "$(GETTEXT "Please try again:")"
		read  KY_ACT_CODE
		if [ "x$KY_ACT_CODE" = "x" ]; then
			ky_activate_retry 9
		else
			kylin_activate "$KY_ACT_CODE"  || ky_activate_retry $?
		fi
	fi

}

ky_activate() {
	echo ""
	: '
	#echo "请输入激活码"
	'
	echo "$(GETTEXT "Please enter the activation code")"
	read  KY_ACT_CODE
	echo ""

	if [ "x$KY_ACT_CODE" = "x" ]; then
		ky_activate_retry 9
	else
		kylin_activate "$KY_ACT_CODE"  || ky_activate_retry $?
	fi
}

show_qrcode() {
	: '
	#echo "输入“kylin”显示注册信息二维码，或直接回车跳过"
	'
	echo "$(GETTEXT "Enter \"kylin\" to display the QR code of registration information, or directly press Enter to skip")"
	sleep 1
	read  SHOW_QR_CODE

	if [ "$SHOW_QR_CODE" = "kylin" ]; then
		echo ""
		clear
		HW_INFO=$(echo $1)
		SERIAL_CODE=$(kylin_serial)
		if [ "$QRCODE_USE_CMD" = "1" ]; then
			QR_CODE_STR=$(/usr/bin/kylin_qrcode 2>/dev/null)
		fi

		if [ "$QR_CODE_STR" = "" ]; then
			QR_CODE_STR="http://wx.kylinos.cn/qywx/distro/activate?f=${SERIAL_CODE}&y=${HW_INFO}"
		fi

		if [ "$QRCODE_TYPE" = "" ]; then
			QRCODE_TYPE="ANSI"
		fi

		echo ""
		qrencode -t "$QRCODE_TYPE" -m 2 "${QR_CODE_STR}" 2> /dev/null && \
			: '
			#echo "注册信息二维码" && \
			'
			echo "$(GETTEXT "Registration information TWO-DIMENSIONAL code")" && \
			sleep 2 && \
			: '
			#echo "请按回车键继续..." && \
			'
			echo "$(GETTEXT "Press Enter to continue...")" && \
			read TMP || \
			sleep 0.1
	fi
}

ky_gen_register() {


	echo ""
	sleep 2
	: '
	#echo "请按回车键继续..."
	'
	echo "$(GETTEXT "Press Enter to continue...")"
	read TMP

	clear
	: '
	#kylin_gen_register > /dev/null || stay_message "注册码生成失败"
	'
	kylin_gen_register > /dev/null || stay_message "$(GETTEXT "Failed to generate the registration QR code")"

	HW_INFO=$(kylin_encrypted_hardware_info)
	show_qrcode "$HW_INFO"

	clear
	CODE_NO_HYPHEN="n"

	REGISTER_NO=$(kylin_gen_register)
	echo $REGISTER_NO

	echo ""
	if [ $SHOW_SERIAL_NO = y ]; then
		: '
		#echo "以上是注册码和序列号，请联系麒麟以获取激活码"
		'
		echo "$(GETTEXT "Above is the registration code and serial number, please contact Kirin for activation code")"
	else
		: '
		#echo "以上是注册码，请联系麒麟以获取激活码"
		'
		echo "$(GETTEXT "Above is the registration code, please contact Kirin to obtain the activation code")"
	fi

	ky_activate
}

ky_verify() {
	clear
	if [ ${readonly} = y ]; then
		: '
		#echo "文件系统只读，无法进行验证。"
		'
		echo "$(GETTEXT "The file system is read-only and cannot be verified")"
		exit 1
	fi

	kylin_activation_check
	if [ $? -ne 0 ];then
		exit 1
	fi

	: '
	#kylin_verify || \
	#	stay_message "系统验证失败"
	'
	kylin_verify || \
		stay_message "$(GETTEXT "System verification failed.")"

	echo ""
	echo ""
	export KYLIN_REALLY_ACTIVATED=y

	ky_gen_register

	unset KYLIN_REALLY_ACTIVATED	
	echo ""
}


ky_verify

unset ROOTFS_DEVICE
unset SHOW_SERIAL_NO
unset CODE_NO_HYPHEN

